// pages/my/order/orderdetail/coupon.js
var QR = require("../../../../utils/qrcode.js");
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qrcStr: '',
    qrcPhld: '维康云u=1001',
    maskHidden: true,
    imagePath: '',
    cinfo:{}
  },
  canvasId: "qrcCanvas", 
  timeerid:0, 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var info = JSON.parse(decodeURIComponent(options.info));
    this.setData({
      cinfo: info
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.size = this.setCanvasSize();//动态设置画布大小  
    var userid = wx.getStorageSync('uid');
    var infostr = 'use:' + this.data.cinfo.consumeid + ':' + this.data.cinfo.buyid + ':' + userid;
    this.createQrCode(infostr, this.canvasId, this.size.w, this.size.h);  

    var that = this;
    this.timeerid = setTimeout(function () {
      that.UseUpdate();
    }, 2000);
  },
  setCanvasSize: function () {
    var size = {};
    try {
      var res = wx.getSystemInfoSync();
      var scale = 750 / 500;//不同屏幕下canvas的适配比例；设计稿是750宽  
      var width = res.windowWidth / scale;
      var height = width;//canvas画布为正方形  
      size.w = width;
      size.h = height;
    } catch (e) {
      // Do something when catch error  
      console.log("获取设备信息失败" + e);
    }
    return size;
  },
  createQrCode: function (url, canvasId, cavW, cavH) {
    //调用插件中的draw方法，绘制二维码图片
    QR.api.draw(url, canvasId, cavW, cavH);

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearTimeout(this.timeerid);
  },
  UseUpdate:function(){
    this.UseToServer();
  },
  UseToServer: function () {
    var userid = wx.getStorageSync('uid');
    var url = app.globalData.webUrl + 'Goods/couponstate_handle';
    var usestr = 'use:userid:' + userid + ':' + this.data.cinfo.consumeid;
    console.log('usestr=' + usestr);
    var tdata = { hand: usestr };

    app.loadDataFromServer(url, this.UseCallBack, tdata);
  },
  UseCallBack: function (res) {

    wx.hideToast();
    if (res.status == 'ok') {
      wx.showToast({
        title: res.msg
      });
      clearTimeout(this.timeerid);
      app.UpdateList_fun('couponlist',true);
      app.UpdateList_fun('order', true);
      return;
    }

    var that = this;
    this.timeerid = setTimeout(function () {
      that.UseUpdate();
    }, 2000);
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearTimeout(this.timeerid);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  loadCouponInfo: function () {
    var coupon = {
      name: '面膜券',
      starttime: '2018-05-25',
      endtime: '2018-06-30',
      qrcode: '../../../imgs/my/qrdemo.png'
    };
    this.setData({
      coupon: coupon
    });
  }
})